package com.example.hlddz2.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import com.example.hlddz2.R;
import java.util.ArrayList;
import java.util.List;

public class SettingActivity extends AppCompatActivity {
    private ListView lvSetting;
    private SettingAdapter adapter;
    private List<String> settingItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        // 初始化设置项数据
        initData();
        // 初始化列表
        initListView();
        // 退出登录按钮点击事件
        initLogoutButton();
    }

    private void initData() {
        settingItems = new ArrayList<>();
        settingItems.add("账号管理");
        settingItems.add("隐私设置");
        settingItems.add("消息通知");
        settingItems.add("关于我们");
        settingItems.add("帮助中心");
    }

    private void initListView() {
        lvSetting = findViewById(R.id.lv_setting);
        adapter = new SettingAdapter();
        lvSetting.setAdapter(adapter);

        // 设置项点击事件
        lvSetting.setOnItemClickListener((parent, view, position, id) -> {
            String title = settingItems.get(position);
            switch (title) {
                case "账号管理":
                    Toast.makeText(this, "跳转到账号管理页面", Toast.LENGTH_SHORT).show();
                    break;
                case "隐私设置":
                    Toast.makeText(this, "跳转到隐私设置页面", Toast.LENGTH_SHORT).show();
                    break;
                case "消息通知":
                    Toast.makeText(this, "跳转到消息通知设置", Toast.LENGTH_SHORT).show();
                    break;
                case "关于我们":
                    Toast.makeText(this, "跳转到关于我们页面", Toast.LENGTH_SHORT).show();
                    break;
                case "帮助中心":
                    Toast.makeText(this, "跳转到帮助中心页面", Toast.LENGTH_SHORT).show();
                    break;
            }
        });
    }

    private void initLogoutButton() {
        Button btnLogout = findViewById(R.id.btn_logout);
        btnLogout.setOnClickListener(v -> {
            new AlertDialog.Builder(this)
                    .setTitle("提示")
                    .setMessage("确定要退出登录吗？")
                    .setPositiveButton("确定", (dialog, which) -> {
                        // 执行退出登录逻辑（清除用户信息、跳转到登录页等）
                        Toast.makeText(this, "已退出登录", Toast.LENGTH_SHORT).show();
                        finish(); // 关闭当前页面
                    })
                    .setNegativeButton("取消", null)
                    .show();
        });
    }

    // 设置列表适配器
    class SettingAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return settingItems.size();
        }

        @Override
        public Object getItem(int position) {
            return settingItems.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = View.inflate(SettingActivity.this, R.layout.item_setting, null);
            }
            TextView tvTitle = convertView.findViewById(R.id.tv_title);
            tvTitle.setText(settingItems.get(position));
            return convertView;
        }
    }
}